<template>
  <MesBox v-model="visible" title="编辑" :width="1200" :height="500">
    <MesForm :formFields="formFields" :formRules="formRules"></MesForm>
    <template #footer>
      <el-button type="primary" @click="saveData">
        <i class="el-icon-success"></i>
        保存
      </el-button>
    </template>
  </MesBox>
</template>

<script setup>
import MesBox from '@/components/basic/MesBox.vue'
import MesForm from '@/components/basic/MesForm.vue'
import { getCurrentInstance, ref } from 'vue'
import { getDatakey } from '@/uitils/utils'

const emit = defineEmits(['parentCall'])
const that = getCurrentInstance().appContext.config.globalProperties
let visible = ref(false)
let formFields = ref({
  MoCode: '',
  WoCodeChild: '',
  ProductCode: '',
  ProductName: '',
  PlanQty: '',
  Unit_Id: '',
  CusName: '',
  CusCode: '',
  PlanStartDate: '',
  PlanEndDate: '',
  Remark: '',
  WorkOrderChildren_Id: '',
})
const formRules = [
  [
    {
      title: '订单号',
      field: 'MoCode',
      readonly: true,
    },
    {
      title: '工单号',
      field: 'WoCodeChild',
      readonly: true,
    },
  ],
  [
    {
      title: '产品编码',
      field: 'ProductCode',
      readonly: true,
    },
    {
      title: '产品名称',
      field: 'ProductName',
      readonly: true,
    },
  ],
  [
    {
      title: '计划数量',
      field: 'PlanQty',
      type: 'number',
    },
    {
      title: '单位',
      field: 'Unit_Id',
      readonly: true,
    },
  ],
  [
    {
      title: '客户名称',
      field: 'CusName',
      readonly: true,
    },
    {
      title: '客户编码',
      field: 'CusCode',
      readonly: true,
    },
  ],
  [
    {
      title: '计划开始时间',
      field: 'PlanStartDate',
      type: 'datetime',
    },
    {
      title: '计划结束时间',
      field: 'PlanEndDate',
      type: 'datetime',
    },
  ],
  [
    {
      title: '备注',
      field: 'Remark',
      type: 'textarea',
    },
  ],
]
const open = async (row) => {
  visible.value = true
  Object.keys(formFields.value).forEach((key) => {
    formFields.value[key] = row[key]
  })
  const res = await getDatakey(['unitList'])
  res[0].data.some((item) => {
    if (item.key == formFields.value.Unit_Id) {
      formFields.value.Unit_Id = item.value
      return true
    }
  })
  // console.log(res)
}

const saveData = async () => {
  const res = await that.http.post(
    '/api/Production_WorkOrder/UpdateWorkChildren',
    formFields.value,
    true
  )
  if (res.status) {
    that.$message.success('修改成功')
    visible.value = false
    emit('parentCall', ($parent) => {
      $parent.search()
    })
  } else {
    that.$message.success(res.message)
  }
}

defineExpose({
  open,
})
</script>

<style lang="less" scoped></style>
